package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.exception.BankCardNotExsitException;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.exception.ConsumerImportRepeatException;
import com.cjack.frenchexcel.service.MoonCardService;
import com.cjack.frenchexcel.service.consumer.MooncardConsumer;
import com.cjack.frenchexcel.util.DateFormatUtil;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 对接mooncard平台
 */
@RestController
@RequestMapping("/mooncardClient")
public class MooncardEndpoint {
    private final static Logger LOG = LogManager.getLogger( MooncardEndpoint.class);

    @Autowired
    MoonCardService moonCardService;
    @Autowired
    MooncardConsumer mooncardConsumer;

    /**
     * 从excel里面导入数据，分成两步来弄
     * 1、先调用upload接口上传excel文件到服务器
     * 2、用第一步上传返回的excel地址调用此接口
     * 从excel导入数据
     * @param filePath  excel的浏览器端访问路径
     * @return
     */
    @RequestMapping(value = "/importFromExcel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult importFromExcel(HttpSession session, @RequestParam("filePath") String filePath) {
        if( StringUtils.isEmpty( filePath)){
            throw new CommonException(ErrorCodeDefine.INTERFACE_COMMON_ERROR, "filePath不能为空");
        }
        Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
        //注意pageDate里面的页数是从0开始计算的
        JSONObject json = moonCardService.importFromExcel( filePath);
        //返回重复的数据
        return AjaxResult.SUCCESS( json);
    }

    /**
     * 主动同步数据，同步数据成功了之后需要重新加载表格
     * 同步今天0点到现在的数据
     * @return
     */
    @RequestMapping(value = "/syncData", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult syncData() {
        try{
            String startTime = DateFormatUtil.format( DateFormatUtil.getLastDayZero( 1), DateFormatUtil.DATE_RULE_6);
            String endTime = DateFormatUtil.format( new Date(), DateFormatUtil.DATE_RULE_6);
            moonCardService.handleExpenses( startTime, endTime);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 获取对账数据
     * @return
     */
    @RequestMapping(value = "/getAccountTotal", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getAccountTotal() {
        try{
            Double total = moonCardService.getAccountTotal();
            return AjaxResult.SUCCESS( total);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
